<?php

namespace App\Http\Middleware;

use Closure;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use mszl\api\utils\ApiUtils;
use Symfony\Component\HttpFoundation\Response;

class RequestMini
{
    /**
     * 处理传入请求。
     *
     * @param Closure(Request): (Response) $next
     * @throws Exception
     */
    public function handle(Request $request, Closure $next):\Illuminate\Http\Response
    {
        ApiUtils::setParams(ApiUtils::input($request));
        ApiUtils::setQueryParams($request->all());

        $token = $request->header('Authorization');
        if ($token) {
            $uid = (int)ApiUtils::getRedis()->get(env('AUTHORIZATION_TOKEN') . ":" . $token);
            if ($uid) {
                ApiUtils::setUserinfo($uid);
                ApiUtils::setToken($token);
            }
        }

        return $next($request);
    }

    /**
     * 在响应发送到浏览器后处理任务。
     */
    public function terminate(Request $request, Response $response): void
    {

    }
}
